package com.yswy.common.wxOrder;


import com.yswy.common.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class CsjAdUtil {

    private static final Charset UTF_8 = StandardCharsets.UTF_8;



    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass){
        try {
            if (map == null)
                return null;
            Object obj = beanClass.newInstance();
            BeanUtils.populate(obj, map);
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            return null ;
        }


    }

    public static Map<?, ?> objectToMap(Object obj) {
        if(obj == null)
            return null;
        return new org.apache.commons.beanutils.BeanMap(obj);
    }

    public static Map<String,Object> beanToMap(Object object){
        Map<String,Object> map = null;
        try {
            map = new HashMap<String, Object>();

            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (key.compareToIgnoreCase("class") == 0) {
                    continue;
                }
                Method getter = property.getReadMethod();
                Object value = getter!=null ? getter.invoke(object) : null;
                if (value instanceof List) {
                    List<Object> list = (List<Object>) value;
                    String s = "";
                    for (Object o: list) {
                        s+=o+",";
                    }
                    s = s.substring(0,s.length()-1);
                    map.put(key, s);
                } else {
                    map.put(key, value);
                }

            }
            //key 可能会把自己的class 和hashcode编进去，直接去掉
            map.remove("class");

        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            if (map.get(key)==null || map.get(key)==""){
                map.remove(key);
                set = map.keySet();
                it = set.iterator();
            }
        }
        return map;
    }

    public static String mapToStr(Map<String, Object> params ){
        String s = "";
        for (String key : params.keySet()) {
            String encodedStr = null ;
            try {
                encodedStr = URLEncoder.encode(params.get(key).toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            s = s+key+"="+encodedStr+"&";
        }
        s = s.substring(0, s.length()-1);
        return s;
    }

    public static String getSignStr(Map<String, Object> params) {
        List<String> paramStr = new ArrayList<>(params.size());
        for (String key : params.keySet()) {
            paramStr.add(key + "=" + params.get(key));
        }
        Collections.sort(paramStr);
        return StringUtils.join(paramStr, "&");
    }
    public static String generateSign(String content, String algorithm, String securityKey){
        content += securityKey;
        MessageDigest md;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
        return bytesToHex(md.digest(content.getBytes(UTF_8)));
    }
    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }


}
